package com.yidi.quartz.controller;

import com.yidi.common.annotation.Log;
import com.yidi.common.core.controller.BaseController;
import com.yidi.common.core.domain.AjaxResult;
import com.yidi.common.core.page.TableDataInfo;
import com.yidi.common.enums.BusinessType;
import com.yidi.common.utils.poi.ExcelUtil;
import com.yidi.quartz.domain.SysJobLog;
import com.yidi.quartz.service.SysJobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：Xn
 * @ClassName: SysJobLogController
 * @date ：2021-10-19 9:46
 * @describe ：调度日志操作处理
 */
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController extends BaseController {

    @Autowired
    private SysJobLogService sysJobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysJobLog sysJobLog, @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize)
    {
        Page<SysJobLog> findByPage = sysJobLogService.findByPage(pageNum, pageSize, sysJobLog);
        return getDataTable(findByPage);
    }

    /**
     * 导出定时任务调度日志列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysJobLog sysJobLog)
    {
        Page<SysJobLog> findByPage = sysJobLogService.findByPage(0, Integer.MAX_VALUE, sysJobLog);
        ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
        return util.exportExcel(findByPage.getContent(), "调度日志");
    }

    /**
     * 根据调度编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/{jobLogId}")
    public AjaxResult getInfo(@PathVariable Long jobLogId)
    {
        return AjaxResult.success(sysJobLogService.findById(jobLogId));
    }

    /**
     * 删除定时任务调度日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务调度日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobLogIds}")
    public AjaxResult remove(@PathVariable Long[] jobLogIds)
    {
        sysJobLogService.deleteJobLogByIds(jobLogIds);
        return success();
    }

    /**
     * 清空定时任务调度日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "调度日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        sysJobLogService.cleanJobLog();
        return success();
    }
}
